package com.example.demo.controller;

import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/trans")
@RestController
@Slf4j
public class TransController {
    @Autowired
    private UserService userService;
    @Transactional
    @RequestMapping("/r1")
    /**
     *
     * 事务提交
     */
    public Boolean r1(String name,String password){
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
int a = 1/0;
        return true;
    }


    @RequestMapping("/r11")

    public Boolean r11(String name,String password){
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);int a =1/0;
        return true;
    }



    /**
     *
     * 发生运行时异常,事务回滚
     */
    @Transactional
    @RequestMapping("/r2")
    public Boolean r2(String name,String password){
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
        int a = 1/0;
        return true;
    }

    /**
     * 发生运行时异常,对异常捕获,事务提交
     * @param name
     * @param password
     * @return
     */

    @Transactional
    @RequestMapping("/r3")
    public Boolean r3(String name,String password){
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
        try{
            int a = 1/0;
        }catch (Exception e){
            log.error("e:{}",e);

        }

        return true;
    }


    /**
     * 发生运行时异常,对异常捕获,重新抛出,
     * 事务进行回滚
     * @param name
     * @param password
     * @return
     */

    @Transactional
    @RequestMapping("/r4")
    public Boolean r4(String name,String password){
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
        try{
            int a = 1/0;
        }catch (Exception e){
           throw e;

        }

        return true;
    }

    /**
     * 发生运行时异常,对异常捕获,手动设置回滚
     * 事务进行回滚
     * @param name
     * @param password
     * @return
     */

    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String name,String password){
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
        try{
            int a = 1/0;
        }catch (Exception e){
            log.error("异常信息,e:{}",e);
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

        }

        return true;
    }


    /**
     * 非运行时异常
     * 默认:事务提交
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r6")
    public Boolean r6(String name,String password) throws IOException {
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
        if(true){
            throw new IOException();
        }
        return true;

    }


    /**
     * 非运行时异常
     * 默认:事务提交
     * 可以设置回滚:   @Transactional(rollbackFor = IOException.class)
     * @param name
     * @param password
     * @return
     */
    @Transactional(rollbackFor = IOException.class)
    @RequestMapping("/r7")
    public Boolean r7(String name,String password) throws IOException {
        Integer result = userService.registry(name, password);
        log.info("数据影响行数: "+ result);
        if(true){
            throw new IOException();
        }
        return true;

    }


}
